package com.example.mediaandroidjava;

import androidx.appcompat.app.AppCompatActivity;
import androidx.media3.common.MediaItem;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory;
import androidx.media3.exoplayer.source.MediaSource;
import androidx.media3.exoplayer.source.ads.AdsLoader;
import androidx.media3.ui.PlayerView;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

public class AdInsertionActivity extends AppCompatActivity {
    private PlayerView playerView;
    private ExoPlayer player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ad_insertion);
    }

    private void adSupport(String videoUri, Uri adTagUri, Context context, AdsLoader.Provider adsLoaderProvider) {
        MediaItem mediaItem =
                new MediaItem.Builder()
                        .setUri(videoUri)
                        .setAdsConfiguration(
                                new MediaItem.AdsConfiguration.Builder(adTagUri).build())
                        .build();
        ;
        MediaSource.Factory mediaSourceFactory =
                new DefaultMediaSourceFactory(context)
                        .setLocalAdInsertionComponents(adsLoaderProvider, /* adViewProvider= */ playerView);
        ExoPlayer player =
                new ExoPlayer.Builder(context).setMediaSourceFactory(mediaSourceFactory).build();
    }

    private void addAds(String firstVideoUri, Uri adTagUri, String secondVideoUri) {
        MediaItem firstItem =
                new MediaItem.Builder()
                        .setUri(firstVideoUri)
                        .setAdsConfiguration(
                                new MediaItem.AdsConfiguration.Builder(adTagUri).setAdsId(adTagUri).build())
                        .build();
        MediaItem secondItem =
                new MediaItem.Builder()
                        .setUri(secondVideoUri)
                        .setAdsConfiguration(
                                new MediaItem.AdsConfiguration.Builder(adTagUri).setAdsId(adTagUri).build())
                        .build();
        player.addMediaItem(firstItem);
        player.addMediaItem(secondItem);
    }
}